{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import k3d\n",
    "import numpy as np\n",
    "\n",
    "plot = k3d.plot()\n",
    "\n",
    "plot += k3d.vectors(\n",
    "   (0,0,0,0,0,0,0,0,0), \n",
    "   (10,0,0,0,10,0,0,0,10), \n",
    "   colors=(0xff0000, 0xff0000, 0x0000ff, 0x0000ff, 0x00ff00, 0x00ff00), \n",
    "   labels=('x', 'y', 'z')\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - let us avaluate a function of 3 variables on relatively large mesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "T = 1.618033988749895\n",
    "from numpy import sin,cos,pi\n",
    "r = 4.77\n",
    "zmin,zmax = -r,r\n",
    "xmin,xmax = -r,r\n",
    "ymin,ymax = -r,r\n",
    "Nx,Ny,Nz = 80,80,80\n",
    "\n",
    "x = np.linspace(xmin,xmax,Nx)\n",
    "y = np.linspace(ymin,ymax,Ny)\n",
    "z = np.linspace(zmin,zmax,Nz)\n",
    "x,y,z = np.meshgrid(x,y,z,indexing='ij')\n",
    "\n",
    "%time p = 2 - (cos(x + T*y) + cos(x - T*y) + cos(y + T*z) \\\n",
    "               + cos(y - T*z) + cos(z - T*x) + cos(z + T*x))\n",
    "\n",
    "p3d_1 = k3d.marching_cubes(p,xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax, zmin=zmin, zmax=zmax,\\\n",
    "                           level=0.0)\n",
    "plot += p3d_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.display()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "p3d_1.color = 0x114455"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - isolevel can be changed from Python side:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "p3d_1.level=-0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipywidgets import interact, interactive, fixed\n",
    "import ipywidgets as widgets\n",
    "\n",
    "@interact(l=widgets.FloatSlider(value=-.1,min=-3,max=1.1))\n",
    "def g(l):\n",
    "    p3d_1.level=-l\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - to avoid recentering one can disable camera auto fit:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "plot.camera_auto_fit = False\n",
    "plot.grid_auto_fit = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    " - one can add other plots to the same scene:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "p =(x**2+y**2+z**2+2*y-1)*((x**2+y**2+z**2-2*y-1)**2-8*z**2)+16*x*z*(x**2+y**2+z**2-2*y-1)\n",
    "plot += k3d.marching_cubes(p,xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax, zmin=zmin, zmax=zmax, level=0.0,color=0xff0000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time \n",
    "p = x**2 + y**2 - z**2 -0.\n",
    "plot += k3d.marching_cubes(p,xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax, zmin=zmin, zmax=zmax, level=0.0,color=0x00ff00)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
